package com.dfdchem.imd.kaiwu.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.WindowManager;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivity;
import com.dfdchem.imd.kaiwu.constant.RouterPath;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 搜索
 */
@Route(path = RouterPath.SEARCH_ACTIVITY)
public class SearchActivity extends BaseActivity {

    private static final String TAG = "SearchActivity";

    private EditText search;

    private static final String[] hints = {
            "MES系统上线",
            "九大指标检查通报",
            "智慧园区二期启动会顺利召开",
            "热烈欢迎中站区领导莅临多氟多指导检查",
            "关于组织参加焦作市第十届职业技能大赛的通知"
    };

    /**
     * 启动这个Activity的Intent
     * @param context
     * @return Intent
     */
    public static Intent createIntent(Context context) {
        return new Intent(context, SearchActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void initView() {
        search = findView(R.id.search);
    }

    @Override
    public void initData() {
        // 搜索框内容定时更新
        Handler handler = new Handler() {
            public void handleMessage(Message msg) {
                String string = hints[msg.getData().getInt("index")];
                search.setHint(string);
                if (string.length() >= 15) {
                    search.setTextSize(TypedValue.COMPLEX_UNIT_PX,
                            getResources().getDimension(R.dimen.text_size_micro));
                } else {
                    search.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.text_size_small));
                }
                super.handleMessage(msg);
            }
        };

        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                int hitIndex = (int) (new Date().getTime() / 1000 / 3 % hints.length);
                Bundle bundle = new Bundle();
                bundle.putInt("index", hitIndex);
                Message message = new Message();
                message.setData(bundle);
                handler.sendMessage(message);
            }
        };

        new Timer().schedule(task, 0, 1000);
    }

    @Override
    public void initEvent() {
        search.setFocusable(true);
        search.setFocusableInTouchMode(true);
        search.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

}
